<template>
  <div class="deepseek-float" :style="floatStyle">
    <!-- 悬浮按钮 -->
    <div v-if="!isExpanded" class="float-button" @mousedown="startDrag" @touchstart="startDrag" @click="toggleExpand">
      <el-icon><ChatDotRound /></el-icon>
    </div>

    <!-- 展开的浮窗 -->
    <div v-else class="float-window">
      <div class="float-header">
        <span class="title">AI 助手</span>
        <el-icon class="close-icon" @click="toggleExpand"><Close /></el-icon>
      </div>
      
      <div class="chat-container" ref="chatContainer">
        <div v-for="(message, index) in messages" :key="index" 
             :class="['message', message.role === 'user' ? 'user-message' : 'assistant-message']">
          <div class="message-content">{{ message.content }}</div>
        </div>
      </div>

      <div class="input-container">
        <el-input
          v-model="userInput"
          type="textarea"
          :rows="3"
          placeholder="输入您的问题..."
          @keyup.enter.ctrl="sendMessage"
        />
        <el-button type="primary" @click="sendMessage" :loading="loading">
          发送
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { ChatDotRound, Close } from '@element-plus/icons-vue'
import axios from 'axios'

export default {
  name: 'DeepSeekFloat',
  components: {
    ChatDotRound,
    Close
  },
  data() {
    return {
      isExpanded: false,
      userInput: '',
      messages: [
        {
          role: 'system',
          content: 'You are a helpful assistant.'
        }
      ],
      loading: false,
      dragging: false,
      position: { x: 20, y: 80 },
      dragOffset: { x: 0, y: 0 }
    }
  },
  computed: {
    floatStyle() {
      if (this.isExpanded) {
        // 展开时居中显示
        return {
          right: '20px',
          bottom: '80px',
          left: 'auto',
          top: 'auto',
        }
      } else {
        return {
          position: 'fixed',
          left: this.position.x + 'px',
          top: this.position.y + 'px',
          right: 'auto',
          bottom: 'auto',
          zIndex: 1000
        }
      }
    }
  },
  methods: {
    toggleExpand() {
      this.isExpanded = !this.isExpanded
      if (this.isExpanded) {
        this.$nextTick(() => {
          this.scrollToBottom()
        })
      }
    },
    async sendMessage() {
      if (!this.userInput.trim() || this.loading) return

      // 添加用户消息
      this.messages.push({
        role: 'user',
        content: this.userInput.trim()
      })
      const currentMessages = this.messages.slice() // 复制历史消息
      this.userInput = ''
      this.loading = true

      try {
        const response = await axios.post('http://localhost:8080/zwart/user/deepseek', {
          model: 'deepseek-chat',
          messages: currentMessages,
          stream: false
        })
        // 添加助手回复
        if (response.data && response.data.choices && response.data.choices[0]) {
          this.messages.push({
            role: 'assistant',
            content: response.data.choices[0].message.content
          })
        }
      } catch (error) {
        this.messages.push({
          role: 'assistant',
          content: '抱歉，发生了错误，请稍后重试。'
        })
      } finally {
        this.loading = false
        this.$nextTick(() => {
          this.scrollToBottom()
        })
      }
    },
    scrollToBottom() {
      const container = this.$refs.chatContainer
      if (container) {
        container.scrollTop = container.scrollHeight
      }
    },
    startDrag(e) {
      this.dragging = true
      let clientX, clientY
      if (e.type === 'touchstart') {
        clientX = e.touches[0].clientX
        clientY = e.touches[0].clientY
      } else {
        clientX = e.clientX
        clientY = e.clientY
      }
      this.dragOffset.x = clientX - this.position.x
      this.dragOffset.y = clientY - this.position.y
      document.addEventListener('mousemove', this.onDrag)
      document.addEventListener('mouseup', this.stopDrag)
      document.addEventListener('touchmove', this.onDrag)
      document.addEventListener('touchend', this.stopDrag)
    },
    onDrag(e) {
      if (!this.dragging) return
      let clientX, clientY
      if (e.type.startsWith('touch')) {
        clientX = e.touches[0].clientX
        clientY = e.touches[0].clientY
      } else {
        clientX = e.clientX
        clientY = e.clientY
      }
      this.position.x = Math.max(0, Math.min(window.innerWidth - 50, clientX - this.dragOffset.x))
      this.position.y = Math.max(0, Math.min(window.innerHeight - 50, clientY - this.dragOffset.y))
    },
    stopDrag() {
      this.dragging = false
      document.removeEventListener('mousemove', this.onDrag)
      document.removeEventListener('mouseup', this.stopDrag)
      document.removeEventListener('touchmove', this.onDrag)
      document.removeEventListener('touchend', this.stopDrag)
    }
  },
  beforeUnmount() {
    this.stopDrag()
  }
}
</script>

<style scoped>
.deepseek-float {
  position: fixed;
  z-index: 1000;
}

.float-button {
  width: 50px;
  height: 50px;
  background-color: #409eff;
  border-radius: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
  user-select: none;
  touch-action: none;
}

.float-button:hover {
  transform: scale(1.1);
}

.float-button .el-icon {
  font-size: 24px;
  color: white;
}

.float-window {
  width: 300px;
  height: 400px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.float-header {
  padding: 16px;
  background: #409eff;
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.float-header .title {
  font-size: 16px;
  font-weight: bold;
}

.close-icon {
  cursor: pointer;
  font-size: 20px;
}

.chat-container {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  background: #f5f7fa;
}

.message {
  margin-bottom: 16px;
  max-width: 80%;
}

.user-message {
  margin-left: auto;
}

.assistant-message {
  margin-right: auto;
}

.message-content {
  padding: 12px;
  border-radius: 12px;
  word-break: break-word;
}

.user-message .message-content {
  background: #409eff;
  color: white;
}

.assistant-message .message-content {
  background: white;
  border: 1px solid #dcdfe6;
}

.input-container {
  padding: 16px;
  background: white;
  border-top: 1px solid #dcdfe6;
}

.input-container .el-button {
  margin-top: 8px;
  width: 100%;
}

@media (max-width: 600px) {
  .float-window {
    width: calc(100vw - 40px);
    height: calc(100vh - 140px);
    position: fixed;
    top: 20px;
    left: 20px;
    right: 20px;
    bottom: 80px;
  }
}
</style> 